package com.limao.DBProvider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.limao.DBDataClass.HomeAreaPlotDataClass;
import com.limao.DBDataClass.HomeConentDataClass;
import com.limao.DbConnection.DbConnectionManager;
import com.limao.Tool.CacheClass;

public class HomeMenu_ColumProvider {
	
	private static HomeMenu_ColumProvider instance;

	public static HomeMenu_ColumProvider getInstance() {
		if(instance==null) instance=new HomeMenu_ColumProvider();
		return instance;
	}
	
  
	private HomeMenu_ColumProvider() {

	}

	private HomeAreaPlotDataClass encapOrderData(ResultSet rs) throws Exception{
		
		HomeAreaPlotDataClass orderData = new HomeAreaPlotDataClass();

		orderData.plotid=rs.getString("plotid");
		orderData.shengid=rs.getString("shengid");
		orderData.cityid=rs.getString("cityid");
		orderData.areaid=rs.getString("areaid");
		orderData.columid=rs.getString("columid");
		orderData.structureid=rs.getString("structureid");
		orderData.structureabout=rs.getString("structureabout");
		
		return orderData;
	}
			
	
	public ArrayList<HomeAreaPlotDataClass> Load_HomePlot_ByAreaidAndColumid(String areaid,String columid)
	{
		ArrayList<HomeAreaPlotDataClass> list = new ArrayList<HomeAreaPlotDataClass>();
		HomeAreaPlotDataClass tmp = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
			try {
				 
				con = DbConnectionManager.getConnection();
				pstmt = con.prepareStatement("select p.*,s.structureabout from cd_area_homeplot p" +
						" left join cd_sys_homestructure s on s.structureid=p.structureid" +
						" where p.areaid=? and p.columid=? and p.state=1 order by p.sortvalue asc ");
				pstmt.setInt(1, CacheClass.getIntValue(areaid));
				pstmt.setInt(2, CacheClass.getIntValue(columid));
				rs = pstmt.executeQuery();
				while (rs.next()) {
					tmp = encapOrderData(rs);
					list.add(tmp);
				}
				
				
				if(list.size()>0)
				{
					for(HomeAreaPlotDataClass obj:list)
					{
						DbConnectionManager.fastcloseStmt(rs,pstmt);
						
						pstmt = con.prepareStatement("select c.* ," +
								" g.goodsname,g.image as goodsimage," +
								" gc.`name` as catename,gc.image as cateimage," +
								" s.shopname,s.shoplogo" +
								" sc.shopcatename,sc.image as shopcateimage" +
								" ht.themename,ht.image as themeimage " +
								" FROM cd_sys_homecontent c " +
								" left JOIN cd_sys_goods g on (c.type=0 and g.goodsid=c.toItemId_Url) " +
								" left JOIN cd_sys_goodscategory gc on (c.type=1 and gc.cateid=c.toItemId_Url) " +
								" left JOIN cd_sys_shop s on (c.type=2 and s.shopid=c.toItemId_Url) " +
								" left JOIN cd_sys_shopcategory sc on (c.type=3 and sc.shopcateid=c.toItemId_Url) " + 
								" left JOIN cd_sys_hometheme ht on ((c.type=4 or c.type=5) and ht.themeid=c.toItemId_Url) " +
								" where c.plotid=? and c.state=1 order by c.sortvalue asc") ;
						pstmt.setInt(1, CacheClass.getIntValue(obj.plotid));
						rs = pstmt.executeQuery();
						while (rs.next()) {
							
							if(obj.sonList==null) obj.sonList=new ArrayList<HomeConentDataClass>();
							
							HomeConentDataClass tmp2 = new HomeConentDataClass();
							tmp2.contentid=rs.getString("contentid");
							tmp2.plotid=rs.getString("plotid");
							tmp2.toItemId_Url=rs.getString("toItemId_Url");
							int type=rs.getInt("type");
							/**
							0:为跳到某个商品详情页  1:为跳到某大类或小类商品列表页（如精选－百货精选－服装）  2：跳至某个店铺内（店铺商品页）  3:跳至某类店铺列表页(如 专营水果类的店) 4:为跳至某一商品类专题（如爆款）5:跳至某一店铺类专题（如名店）  6跳到某一个网页web
							**/
							switch(type)
							{
							case 0:
								tmp2.itemAbout=rs.getString("goodsname");
								tmp2.itemImageUrl=rs.getString("goodsimage");
								break;
							case 1:
								tmp2.itemAbout=rs.getString("catename");
								tmp2.itemImageUrl=rs.getString("cateimage");
								break;
							case 2:
								tmp2.itemAbout=rs.getString("shopname");
								tmp2.itemImageUrl=rs.getString("shoplogo");
								break;
							case 3:
								tmp2.itemAbout=rs.getString("shopcatename");
								tmp2.itemImageUrl=rs.getString("shopcateimage");
								break;
							case 4:
							case 5:
								tmp2.itemAbout=rs.getString("themename");
								tmp2.itemImageUrl=rs.getString("themeimage");
								break;
							case 6:
								tmp2.itemAbout=rs.getString("itemAbout");
								tmp2.itemImageUrl=rs.getString("itemImageUrl");
								break;
							default:
							
							}
							
							obj.sonList.add(tmp2);
						}
						
						
					}
				}
				

			} catch (Exception e) {
				e.printStackTrace();
				list .clear();
			} finally {
				DbConnectionManager.closeConnection(rs, pstmt, con);
			}
			return list;
	}

}
